﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UAC;
using UAC.Operation;

namespace UACManager
{
    public partial class MainForm : Form
    {
        private string userName;
        public MainForm(string userName)
        {
            InitializeComponent();
            this.userName = userName;
        }

        private List<DocumentDef> DocDefs;
        private void LoadDocemuntDefList()
        {
            try
            {
                this.DocDefs = Utils.ListDocumentDef();
                DataTable result = new DataTable();
                result.Columns.Add("DocDefID");
                result.Columns.Add("DocDefName");
                if (this.DocDefs != null)
                {
                    foreach (DocumentDef docDef in this.DocDefs)
                    {
                        DataRow dr = result.NewRow();
                        dr[0] = docDef.DocumentDefID;
                        dr[1] = docDef.Name;
                        result.Rows.Add(dr);
                    }
                }
                this.dataGridViewMain.DataSource = result;
            }
            catch (Exception)
            {
            }
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            //ControlCondition condition = new ControlCondition();
            //condition.Description = "UACSystem";
            //condition.DocumentDefID = 1000;
            //condition.OperationType = OperationType.Or;

            //ControlCondition child1 = new ControlCondition();
            //child1.OperationType = OperationType.Person;
            //child1.DocumentDefID = 1000;
            //child1.Description = "Admin";
            //child1.Values1 = new ConditionValue() { ValueType = UAC.Operation.ValueType.Constant, Value = "admin" };

            //ControlCondition child2 = new ControlCondition();
            //child2.OperationType = OperationType.Person;
            //child2.DocumentDefID = 1000;
            //child2.Description = "Administrator";
            //child2.Values1 = new ConditionValue() { ValueType = UAC.Operation.ValueType.Constant, Value = "administrator" };

            //ControlCondition child3 = new ControlCondition();
            //child3.OperationType = OperationType.And;
            //child3.DocumentDefID = 1000;
            //child3.Description = "And";

            //ControlCondition child3_1 = new ControlCondition();
            //child3_1.OperationType = OperationType.Role;
            //child3_1.DocumentDefID = 1000;
            //child3_1.Description = "Role";
            //child3_1.Values1 = new ConditionValue() { ValueType = UAC.Operation.ValueType.Constant, Value = 1001 };
            //child3_1.Values2 = new ConditionValue() { ValueType = UAC.Operation.ValueType.Constant, Value = 1001000 };

            //ControlCondition child3_2 = new ControlCondition();
            //child3_2.OperationType = OperationType.Compare;
            //child3_2.DocumentDefID = 1000;
            //child3_2.Description = "Compare";
            //child3_2.Values1 = new ConditionValue() { ValueType = UAC.Operation.ValueType.DocDef, Value = "Company" };
            //child3_2.Values2 = new ConditionValue() { ValueType = UAC.Operation.ValueType.Constant, Value = "Brightmoon" };
            //child3_2.Values3 = new ConditionValue() { ValueType = UAC.Operation.ValueType.Constant, Value = CompareType.Equal };
            //child3_2.Values4 = new ConditionValue() { ValueType = UAC.Operation.ValueType.Constant, Value = ParamType.String };

            //child3.ConditionList = new List<ControlCondition>() { child3_1, child3_2 };

            //condition.ConditionList = new List<ControlCondition>() { child1, child2, child3 };

            //try
            //{
            //    Utils.AddOperationControl(condition);
            //}
            //catch (Exception ex)
            //{
            //}

            LoadDocemuntDefList();
        }

        private void mBtnCreateDocDef_Click(object sender, EventArgs e)
        {
            using (DocDefForm ddf = new DocDefForm())
            {
                if (ddf.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    LoadDocemuntDefList();
                }
            }
        }

        private void mBtnCreateTeamDef_Click(object sender, EventArgs e)
        {
            using (TeamDefForm df = new TeamDefForm())
                df.ShowDialog();
        }

        private void mBtnCreatePeople_Click(object sender, EventArgs e)
        {

        }

        private void dataGridViewMain_DoubleClick(object sender, EventArgs e)
        {
            if (this.dataGridViewMain.SelectedRows != null && this.dataGridViewMain.SelectedRows.Count > 0)
            {
                int documentDefId = Convert.ToInt32(this.dataGridViewMain.SelectedRows[0].Cells[0].Value.ToString());
                using (DocumentForm df = new DocumentForm(documentDefId))
                    df.ShowDialog();
            }
        }

        private void mBtnTeamDefList_Click(object sender, EventArgs e)
        {
            using (TeamDefListForm dfl = new TeamDefListForm())
                dfl.ShowDialog();
        }
    }
}
